﻿using Newtonsoft.Json;
using XYS.Core.Util;
using XYS.Service.Core.Models;
using XYS.Service.Core.Service;

namespace XYS.Core.Models;

/// <summary>
/// 当前登录的用户
/// </summary>
public class CurrentUserInfo
{
    /// <summary>
    /// 跟踪Id（每次请求都会变,同一个请求则相同）
    /// </summary>
    public string TrackId { get; set; }
    /// <summary>
    /// 设备指纹
    /// </summary>
    public string DeviceFingerprint { get; set; }
    /// <summary>
    /// 是否模拟登录
    /// </summary>
    public bool IsFake { get; set; }
    /// <summary>
    /// 用户登录账号Id
    /// </summary>
    public long AccountId { get; set; }
    /// <summary>
    /// 用户登录账号Id-字符串
    /// </summary>
    public string AccountIdString => AccountId.ToString();
    /// <summary>
    /// 当前组织
    /// </summary>
    public long OrgId { get; set; }
    /// <summary>
    /// 当前组织-字符串
    /// </summary>
    public string OrgIdString => OrgId.ToString();
    /// <summary>
    /// 昵称
    /// 冗余字段
    /// </summary>
    public string NickName { get; set; }
    /// <summary>
    /// 用户名
    /// </summary>
    public string UserName { get; set; }
    /// <summary>
    /// 手机号
    /// </summary>
    public string Phone { get; set; }
    /// <summary>
    /// 邮箱地址
    /// </summary>
    public string Email { get; set; }
    /// <summary>
    /// 有效期
    /// </summary>
    public DateTime? ValidTime { get; set; }

    /// <summary>
    /// 拥有的业务权限
    /// </summary>
    public EnumActivateService? ActivateService { get; set; } = EnumActivateService.Nothing;
    /// <summary>
    /// 当前组织信息
    /// </summary>
    public CurrentUserOrgResult OrgInfo { get; set; }


    /// <summary>
    /// 角色集合（默认不加载）需要加载的话请实现接口：ICurrentUserService
    /// </summary>
    [JsonIgnore]
    public List<CurrentUserRoleResult> Roles
    {
        get
        {
            if (roles != null && roles.Count > 0) return roles;
            CurrentUserService?.LoadRoles(this);
            return roles;
        }
        set => roles = value;
    }

    /// <summary>
    /// 所在部门（默认不加载）需要加载的话请实现接口：ICurrentUserService
    /// </summary>
    [JsonIgnore]
    public List<CurrentUserDeptResult> Depts
    {
        get
        {
            if (depts != null && depts.Count > 0) return depts;
            CurrentUserService?.LoadDepts(this);
            return depts;
        }
        set => depts = value;
    }

    /// <summary>
    /// 拥有的菜单（默认不加载）需要加载的话请实现接口：ICurrentUserService
    /// </summary>
    [JsonIgnore]
    public List<CurrentMenusResult> Menus
    {
        get
        {
            if (menus != null && menus.Count > 0) return menus;
            CurrentUserService?.LoadMenus(this);
            return menus;
        }
        set => menus = value;
    }

    /// <summary>
    /// token
    /// </summary>
    public string Token { get; set; }

    /// <summary>
    /// 创建一个匿名用户
    /// </summary>
    private static readonly CurrentUserInfo AnonymousUser = new()
    {
        NickName = "匿名用户",
    };
    private List<CurrentUserRoleResult> roles = new();
    private List<CurrentUserDeptResult> depts = new();
    private List<CurrentMenusResult> menus = new();

    /// <summary>
    /// 创建一个匿名用户信息
    /// </summary>
    /// <returns></returns>
    public static CurrentUserInfo CreateAnonymousUser() => AnonymousUser;

    /// <summary>
    /// 相关服务接口（用于获取 角色、菜单、权限）
    /// </summary>
    public static ICurrentUserService CurrentUserService { get; set; }
}

/// <summary>
/// 当前用户所拥有的角色
/// </summary>
public class CurrentUserRoleResult
{
    /// <summary>
    /// 角色Id
    /// </summary>
    public long RoleId { get; set; }
    /// <summary>
    /// 组织 Id
    /// </summary>
    public long OrgId { get; set; }
    /// <summary>
    /// 是否为管理员角色，管理员角色拥有当前组织下所有权限
    /// </summary>
    public bool IsAdmin { get; set; } = false;

    /// <summary>
    /// 角色名
    /// </summary>
    public string RoleName { get; set; }
}

/// <summary>
/// 当前用户所在的部门
/// </summary>
public class CurrentUserDeptResult
{
    /// <summary>
    /// 部门 Id
    /// </summary>
    public long Id { get; set; }
    /// <summary>
    /// 组织 Id
    /// </summary>
    public long OrgId { get; set; }
    /// <summary>
    /// 部门名
    /// </summary>
    public string DeptName { get; set; }
    /// <summary>
    /// 部门负责人 id
    /// </summary>
    public long? DeptLeaderId { get; set; }
    /// <summary>
    /// 父部门 Id
    /// </summary>
    public long ParentId { get; set; }

    /// <summary>
    /// 部门职责
    /// </summary>
    public EnumDeptService DeptService { get; set; }

}

/// <summary>
/// 当前用户所拥有的组织
/// </summary>
public class CurrentUserOrgResult
{
    /// <summary>
    /// id
    /// </summary>
    public long Id { get; set; }
    /// <summary>
    /// 组织类型 （1：机构，2：企业，99：admin后台）
    /// </summary>
    public EnumOrgType OrgType { get; set; }
    /// <summary>
    /// 组织属性
    /// </summary>
    public EnumOrgAttribute OrgAttribute => OrgType switch
    {
        EnumOrgType.Org => ParentId <= 0 ? EnumOrgAttribute.Org : EnumOrgAttribute.OrgSub,
        EnumOrgType.Ent => ParentId <= 0 ? EnumOrgAttribute.Ent : EnumOrgAttribute.EntSub,
        EnumOrgType.Expert => EnumOrgAttribute.Expert,
        EnumOrgType.Admin => EnumOrgAttribute.Admin,
        EnumOrgType.Member => EnumOrgAttribute.Member,
        _ => EnumOrgAttribute.Unknown
    };

    /// <summary>
    /// 组织名称
    /// </summary>
    public string OrgName { get; set; }

    /// <summary>
    /// 组织代码（机构代码/统一社会信用代码/身份证号）
    /// </summary>
    public string OrgCode { get; set; }

    /// <summary>
    /// 父组织 id
    /// </summary>
    public long ParentId { get; set; }

    /// <summary>
    /// 激活的服务 二进制(0x00000000)
    /// 0x00000001  工作流
    /// 0x00000010  专利
    /// 0x00000100  项目
    /// 0x00001000  商标
    /// 0x00010000  版权
    /// </summary>
    public EnumActivateService ActivateServices { get; set; }

    /// <summary>
    /// 企业版会员等级
    /// </summary>
    public EnumEntVIPLevel EntVIPLevel { get; set; }
}


/// <summary>
/// 当前用户拥有的菜单集合
/// </summary>
public class CurrentMenusResult
{
    /// <summary>
    /// 菜单 id
    /// </summary>
    public required long MenuId { get; set; }
    /// <summary>
    /// 菜单名
    /// </summary>
    public required string MenuName { get; set; }
    /// <summary>
    /// 菜单名
    /// </summary>
    public required string MenuTitle { get; set; }
    /// <summary>
    /// 父菜单 id
    /// </summary>
    public required long ParentId { get; set; }

    /// <summary>
    /// 权限列表
    /// </summary>
    public List<CurrentActionsResult> Actions { get; set; }
}

/// <summary>
/// 当前用户拥有的所有业务权限
/// </summary>
public class CurrentActionsResult
{
    /// <summary>
    /// 菜单 id
    /// </summary>
    public required long MenuId { get; set; }
    /// <summary>
    /// 权限id
    /// </summary>
    public required long ActionId { get; set; }
    /// <summary>
    /// 权限名，用于鉴权
    /// </summary>
    public required string ActionName { get; set; }
    /// <summary>
    /// 权限标题
    /// </summary>
    public required string ActionTitle { get; set; }
}
